package model;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
import java.util.LinkedList;

/**
 * UserRank: tum0r
 * Package Name: model
 * Time: 2019-01-28 Monday 16:12:32
 */
public class UserRank implements Comparator<UserRank> {

    private String userName = "";
    private int solveCount = 0;
    private int notSolveCount = 0;
    private int allCount = 0;
    private String lastSubmitTime = "";
    private long lastSubmit = 0;
    private long punishedTime = 0;
    private String allTime = "";
    private long all = 0;

    public String getUserName() {
        return userName;
    }

    public void userName(String userName) {
        this.userName = userName;
    }

    public int getSolveCount() {
        return solveCount;
    }

    public void setSolveCount(int solveCount) {
        this.solveCount = solveCount;
    }

    public int getNotSolveCount() {
        return notSolveCount;
    }

    public void setNotSolveCount(int notSolveCount) {
        this.notSolveCount = notSolveCount;
    }

    public int getAllCount() {
        return allCount;
    }

    public void setAllCount(int allCount) {
        this.allCount = allCount;
    }

    public String getLastSubmitTime() {
        return lastSubmitTime;
    }

    public void setLastSubmitTime(String lastSubmitTime) {
        this.lastSubmitTime = lastSubmitTime;
    }

    public long getLastSubmit() {
        return lastSubmit;
    }

    public void setLastSubmit(long lastSubmit) {
        this.lastSubmit = lastSubmit;
    }

    public long getPunishedTime() {
        return punishedTime;
    }

    public void setPunishedTime(long punishedTime) {
        this.punishedTime = punishedTime;
    }

    public String getAllTime() {
        return allTime;
    }

    public void setAllTime(String allTime) {
        this.allTime = allTime;
    }

    public long getAll() {
        return all;
    }

    public void setAll(long all) {
        this.all = all;
    }

    public UserRank() {
    }

    public UserRank(String userName, int solveCount, int notSolveCount, String gameStart, String lastSubmitTime, String punish) {
        this.userName = userName;
        this.solveCount = solveCount;
        this.notSolveCount = notSolveCount;
        this.lastSubmitTime = lastSubmitTime;
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
            long gameStartTime = simpleDateFormat.parse(gameStart).getTime();
            String[] punishTime = punish.split(":");
            long punishHours = 0;
            long punishMinutes = 0;
            try {
                punishHours = Long.parseLong(punishTime[0]) * 3600000;
            } catch (Exception e) {
            }
            try {
                punishMinutes = Long.parseLong(punishTime[1]) * 60000;
            } catch (Exception e) {
            }
            punishedTime = notSolveCount * (punishHours + punishMinutes);
            if (!lastSubmitTime.equals("")) {
                simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                lastSubmit = simpleDateFormat.parse(lastSubmitTime).getTime();
                all = lastSubmit + punishedTime - gameStartTime;
            } else {
                lastSubmit = 0;
                all = punishedTime;
            }
            String hours = String.valueOf(all / 3600000);
            String minutes = String.valueOf((all % 3600000) / 60000);
            String second = String.valueOf(((all % 3600000) % 60000) / 1000);
            allTime = (hours.length() == 1 ? "0" + hours : hours) + ":" + (minutes.length() == 1 ? "0" + minutes : minutes) + ":" + (second.length() == 1 ? "0" + second : second);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    public int compare(UserRank userRank1, UserRank userRank2) {
        if (userRank1.getSolveCount() != userRank2.getSolveCount()) {
            return Integer.compare(userRank2.getSolveCount(), userRank1.getSolveCount());
        } else {
            return Long.compare(userRank1.getAll(), userRank2.getAll());
        }
    }

}
